<?php
namespace Admin\Controller;
use Think\Controller;
class CatController extends Controller {
    public $CatModel;
    public function __construct(){
        parent::__construct();
        $this->CatModel = D('Cat');
    }
    public function cateadd(){
        if(!IS_POST){
            $this->display();
        }else {
            $this->CatModel->cat_name = I('post.cat_name');
            $this->CatModel->parent_id = I('post.parent_id');
            $this->CatModel->intro = I('post.intro');
            if($this->CatModel->add()){
                //跳转列表
                $this->redirect('Admin/cat/catelist');
            }else{
                echo 'shibai';
            }
        }
    }
    public function catelist(){
        $catInfo = S('cats');
        if($catInfo === false){
            echo 'mysql';
            $catInfo = $this->CatModel->getTree();
            S('cats',$catInfo,10);
        }

        // $catInfo = $this->CatModel->select();
        $this->assign('catInfo',$catInfo);
        $this->display();
    }

    public function catdel(){
        $cat_id = I('get.cat_id');
        if($this->CatModel->delete($cat_id)){
            $this->success('删除成功',U('Admin/cat/catelist'),3);
        }else{
            echo 'shazi，写错了，删除都能失败，你还能干啥';
        }
    }

    public function edit(){
        $cat_id = I('get.cat_id');

        if(IS_POST){

            $this->CatModel->where('cat_id='.I('post.cat_id'))->save($_POST);
        }else {
            $cat_info = $this->CatModel->find($cat_id);
            $this->assign('cat_info',$cat_info);
            $this->display();
        }


    }

}
